//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

//位取反运算符
let initialBits:UInt8 = 0b00001111
let invertedBits = ~initialBits                 //equals ob11110000

//位与运算符 只有当两个数都为1 才返回1
let firstSixBits:UInt8 = 0b11111100
let lastSixBits:UInt8 = 0b00111111
let middleFourBits = firstSixBits & lastSixBits //equals ob00111100

//位或运算符 两个数任一一个为1 则返回1
let someBits:UInt8 = 0b10110010
let moreBits:UInt8 = 0b01011110
let combineBits = someBits | moreBits           //equals 0b11111110

//位异或运算符 比特位相同返回0 比特位不同返回1
let firstBits:UInt8 = 0b00010100
let otherBits:UInt8 = 0b00000101
let outputBits = firstBits ^ otherBits          //equals 0b00010001

//位左移和位右移运算符
let shiftBits:UInt8 = 4 //00000100
shiftBits >> 1          //00000010
shiftBits << 2          //00001000

let pink:UInt32 = 0xCC6699  //equale 0b110011000110011010011001
let redComponent = (pink & 0xFF0000) >> 16   //equale 0b000000000000000011001100
let greenComponent = (pink & 0x00FF00) >> 8  //equale 0b000000000000000001100110
let blueComponent = (pink & 0x0000FF)        //equale 0b000000000000000010011001


//有符号整型的位移操作
let a:Int8 = 0b01111100
let a1 = 0b01111110
let b:Int8 = 0b01111111
let c:Int8 = 0b01111011

//值溢出
var unsignedOverflow = UInt8.max
unsignedOverflow = unsignedOverflow &+ 1

var unsignedOverMin = UInt8.min
unsignedOverMin = unsignedOverMin &- 1

var OverMin = Int8.min
OverMin = OverMin &- 1

//优先级 结合性
2+3%4*5

//运算符函数
struct Vector2D{
    var x = 0.0,y = 0.0
}
extension Vector2D{
    static func +(left:Vector2D,right:Vector2D)->Vector2D{
        return Vector2D(x: left.x+right.x, y: left.y+right.y)
    }
}
let vector = Vector2D(x: 3.0, y: 1.0)
let anotherVector = Vector2D(x: 2.0, y: 4.0)
let combinedVector = vector + anotherVector
combinedVector.x
combinedVector.y

//前缀和后缀运算符
extension Vector2D{
    static prefix func -(vector:Vector2D)->Vector2D{
        return Vector2D(x: -vector.x, y: -vector.y)
    }
}
let positive = Vector2D(x: 3.0, y: 4.0)
let negative = -positive
negative.x
negative.y

extension Vector2D{
    static postfix func ++(vector:Vector2D)->Vector2D{
        return Vector2D(x: vector.x + 1, y: vector.y + 1)
    }
}
let vec1 = Vector2D(x: 2.0, y: 2.0)
let vec2 = vec1++
vec2.x
vec2.y

//组合赋值运算符
extension Vector2D{
    static func += (left:inout Vector2D,right: Vector2D){
        left = left + right
    }
}
var original = Vector2D(x: 1.0, y: 2.0)
var vectorToAdd = Vector2D(x: 3.0, y: 4.0)
original += vectorToAdd
original.x
original.y

//等价运算符
extension Vector2D{
    static func == (left:Vector2D,right:Vector2D)->Bool{
        return (left.x == right.x) && (left.y == right.y)
    }
}
extension Vector2D {
    static func != (left:Vector2D,right:Vector2D)->Bool{
        return !(left == right)
    }
}
let twoThree = Vector2D(x: 2.0, y: 3.0)
let anotherTwoThree = Vector2D(x: 2.0, y: 3.0)
twoThree == anotherTwoThree

//自定义运算符
prefix operator +++
extension Vector2D{
    static prefix func +++(vector:inout Vector2D)->Vector2D{
        vector += vector
        return vector
    }
}
var toBeDouble = Vector2D(x: 1.0, y: 4.0)
+++toBeDouble
toBeDouble.x
toBeDouble.y

//自定义中缀运算符的优先级和结合性
infix operator +- {associativity left precedence 140}
extension Vector2D{
    static func +- (left:Vector2D,right:Vector2D)->Vector2D{
        return Vector2D(x: left.x + right.x, y: left.y - right.y)
    }
}
let firstVector = Vector2D(x: 1.0, y: 2.0)
let secondVector = Vector2D(x: 3.0, y: 4.0)
let plusVector = firstVector +- secondVector
plusVector.x
plusVector.y







